/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.impl.DaoPronosticoImp;
import service.xml.Xml;

/*
 * ServletRepAlt
 *
 * Descripción: Esta clase se encarga del update del pronostico y las vistas
 *
 * Autor: Erik
 *
 * Versión: 0.1
 *
 * Fecha: 05/05/14
 *
 */
public class ServletRepAlt extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String columt[] = { " varchar (255) NULL,"};       
        try {
            String idc = request.getParameter("idc");
            String id = request.getParameter("id");
            String vv[] = idc.split(",");
            String vec[] = {"", "", "", "","","",  ""}; // 0 String scolm ="";       2 //  String on ="";    
            vec[5]=id;
            for (int i = 0; i < vv.length; i++) {
                
                String temp[] =   vv[i].split(";");
                vec[0] += "d.[" + temp[0] + "],";
                vec[6] += "SxD.[" + temp[1] + "],";
                vec[2] += "d.[" + temp[0] + "]=SxD.[" + temp[0] + "] AND ";
                vec[4] += "d.[" + temp[0] + "]=SxD.[" + temp[1] + "] AND ";                
                vec[3] += "["+ temp[0] +"],";
            }
            idc = request.getParameter("idf");
            vv = idc.split(",");
            for (int i = 0; i < vv.length; i++) {
                if (i == (vv.length - 1)) {
                    vec[1] = " AND (" + vec[1] + " d.Anho_Semana=''" + vv[i] + "'' )";
                } else {
                    vec[1] += "d.Anho_Semana=''" + vv[i] + "'' OR ";
                }

            }
            (new DaoPronosticoImp()).UPD_Vistas(vec);
             List<Object[]> list = new ArrayList<Object[]>();
            String[] titu = {"Mensaje"};
            Object[] fila = {"Se actualizo correctamente"};
            list.add(titu);
            list.add(fila);
            Xml xml = new Xml();
            StringBuilder result = xml.toXml(list);
            response.setContentType("text/xml;charset=UTF-8");
            PrintWriter out = response.getWriter();            
            out.print(result);
        } catch (Exception e) {
            System.out.println("" + e.getMessage());
        }


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
